Skip to content

Conversation

lastobelus
Copy link

this in conjunction with using a scope that joins
an association allows fetching attributes of associated
records.

class Organization
  belongs_to :contact

  scope :with_contact, -> { joins(:contact) }
end

autocomplete :organization, :name, extra_data: ['contacts.email'],
select_method: :pluck, scopes: [:with_contact]

f.autocomplete_field :name,
autocomplete_organization_name_product_path, update_elements: {id:
'#products_organization_attributes_id', 'contacts.email':
'#products_contact_attributes_email'}

I have not yet written any tests, let me know if you are interested in including this and I will try to provide some coverage.

this in conjunction with using a scope that joins
an association allows fetching attributes of associated
records.

class Organization
  belongs_to :contact

  scope :with_contact, -> { joins(:contact) }
end

autocomplete :organization, :name, extra_data: ['contacts.email'],
select_method: :pluck, scopes: [:with_contact]

f.autocomplete_field :name,
autocomplete_organization_name_product_path, update_elements: {id:
'#products_organization_attributes_id', 'contacts.email':
'#products_contact_attributes_email'}
@@ -77,6 +79,7 @@ class AutocompleteTest < Test::Unit::TestCase
should 'add that extra data to result' do
item = mock(Object)
mock(item).send(:name).times(2) { 'Object Name' }
stub(item).kind_of?(Hash){ false }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer Object#is_a? over Object#kind_of?.
Space missing to the left of {.

@bigtunacan
Copy link
Collaborator

@lastobelus Yeah; this looks useful. If you want to add a couple of test cases and some documentation to the readme I'll merge it in.

Thanks

because rails internally does a uniq before querying
item["label"] = item["value"] = item[method.to_s]
end
else
items = items.collect do |item|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer map over collect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants